package com.ruoyi.project.system.material.center.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.material.center.domain.MaterialCenter;
import com.ruoyi.project.system.material.center.service.IMaterialCenterService;
import com.ruoyi.project.system.material.img.domain.MaterialImg;
import com.ruoyi.project.system.material.img.service.IMaterialImgService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 素材中心Controller
 * 
 * @author LCL
 * @date 2021-01-21
 */
@Controller
@RequestMapping("/system/material")
public class MaterialCenterController extends BaseController
{
    private String prefix = "system/material";

    @Autowired
    private IMaterialCenterService materialCenterService;
    @Autowired
    private IMaterialImgService materialImgService;

    @RequiresPermissions("system:material:view")
    @GetMapping()
    public String material()
    {
        return prefix + "/material";
    }

    /**
     * 查询素材中心列表
     */
    @RequiresPermissions("system:material:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MaterialCenter materialCenter)
    {
        startPage();
        List<MaterialCenter> list = materialCenterService.selectMaterialCenterList(materialCenter);
        return getDataTable(list);
    }

    /**
     * 导出素材中心列表
     */
    @RequiresPermissions("system:material:export")
    @Log(title = "素材中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MaterialCenter materialCenter)
    {
        List<MaterialCenter> list = materialCenterService.selectMaterialCenterList(materialCenter);
        ExcelUtil<MaterialCenter> util = new ExcelUtil<MaterialCenter>(MaterialCenter.class);
        return util.exportExcel(list, "material");
    }

    /**
     * 新增素材中心
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存素材中心
     */
    @RequiresPermissions("system:material:add")
    @Log(title = "素材中心", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MaterialCenter materialCenter, String[] imgBannerUrl, String[] imgBannerRealPath)
    {
        materialCenterService.insertMaterialCenter(materialCenter);
        if(imgBannerUrl!=null){
            //素材中心图
            for (int i = 0; i < imgBannerUrl.length; i++) {
                MaterialImg materialImg = new MaterialImg();
                materialImg.setSort(Integer.toUnsignedLong(i));
                materialImg.setMaterialId(materialCenter.getId());
                materialImg.setUrl(imgBannerUrl[i]);
                materialImg.setUrlRealPath(imgBannerRealPath[i]);
                materialImg.setType(0);
                this.materialImgService.insertMaterialImg(materialImg);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改素材中心
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        MaterialCenter materialCenter = materialCenterService.selectMaterialCenterById(id);
        mmap.put("materialCenter", materialCenter);
        mmap.put("goodsBanner", this.materialImgService.findListByMaterialIdAndType(id,0));
        return prefix + "/edit";
    }

    /**
     * 修改保存素材中心
     */
    @RequiresPermissions("system:material:edit")
    @Log(title = "素材中心", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MaterialCenter materialCenter, String[] imgBannerUrl, String[] imgBannerRealPath)
    {
        materialCenterService.updateMaterialCenter(materialCenter);
        if(imgBannerUrl!=null){
            materialImgService.deleteByMaterialId(materialCenter.getId());
            //素材中心图
            for (int i = 0; i < imgBannerUrl.length; i++) {
                MaterialImg materialImg = new MaterialImg();
                materialImg.setSort(Integer.toUnsignedLong(i));
                materialImg.setMaterialId(materialCenter.getId());
                materialImg.setUrl(imgBannerUrl[i]);
                materialImg.setUrlRealPath(imgBannerRealPath[i]);
                materialImg.setType(0);
                this.materialImgService.insertMaterialImg(materialImg);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 删除素材中心
     */
    @RequiresPermissions("system:material:remove")
    @Log(title = "素材中心", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(materialCenterService.deleteMaterialCenterByIds(ids));
    }
}
